package cn.com.dashihui.pay.wx.kit;

import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import com.jfinal.kit.PropKit;

import cn.com.dashihui.pay.alipay.sign.Base64;


public class AESUtil {
	 /** 
     * 密钥算法 
     */  
    private static final String ALGORITHM = "AES";  
    /** 
     * 加解密算法/工作模式/填充方式 
     */  
    private static final String ALGORITHM_MODE_PADDING = "AES/ECB/PKCS7Padding"; 
    private static  Boolean initialized = false;  
    /** 
     * AES加密 
     *  
     * @param data 
     * @return 
     * @throws Exception 
     */  
//    public static String encryptData(String data) throws Exception {  
//        // 创建密码器  
//        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);  
//        // 初始化  
//        cipher.init(Cipher.ENCRYPT_MODE, key);  
//        return Base64.encode(cipher.doFinal(data.getBytes()));  
//    }  
  
    /** 
     * AES解密 
     *  
     * @param base64Data 
     * @return 
     * @throws Exception 
     */  
    public static String decryptData(String base64Data,String key1) throws Exception { 
    	initialize();
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);  
        SecretKeySpec key = new SecretKeySpec(MD5.MD5Encode(key1).getBytes(), ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);  
        return new String(cipher.doFinal(Base64.decode(base64Data)),"UTF-8");  
    }  
    public static void initialize(){  
        if (initialized) return;  
        Security.addProvider(new BouncyCastleProvider());  
        initialized = true;  
    }  
}

